﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Project.Module.DAO.IBL;
using Project.Module.Model;
using Project.Engine.Utilities;

namespace Project.Module.DAO.BL
{
    public class SearchBL : IBLTable<Product, int>
    {
        public List<ProductModel> SearchProduct(int cateId, string keyword, int start, int limit, ref int total)
        {
            if (cateId != 0)
            {
                var query = from e in this.Context.Products
                            where e.CategoryID == cateId
                            && e.ProductName.Contains(keyword) == true
                            select new ProductModel()
                            {
                                CatagoryId = e.CategoryID,
                                ProductId = e.ProductID,
                                Description = e.Description,
                                ImageID = e.ImageID != null ? e.ImageID.Value : 0,
                                CreatedDate = e.CreatedDate.HasValue ? e.CreatedDate.Value : Convert.ToDateTime("01/01/1900"),
                                CreatedBy = e.CreatedBy,
                                ProductStatus = e.ProductStatus == true ? true : false, // true: sẵn sàng
                                ProductName = e.ProductName,
                                priceProduct = e.Price,
                                quantityProduct = e.Quantity,
                                money = e.Price * e.Quantity,
                            };

                total = query.Count();

                //return
                return query.Take(total).ToList();
            }
            else
            {
                var query = from e in this.Context.Products
                            where e.ProductName.Contains(keyword) == true
                            select new ProductModel()
                            {
                                CatagoryId = e.CategoryID,
                                ProductId = e.ProductID,
                                Description = e.Description,
                                ImageID = e.ImageID != null ? e.ImageID.Value : 0,
                                CreatedDate = e.CreatedDate.HasValue ? e.CreatedDate.Value : Convert.ToDateTime("01/01/1900"),
                                CreatedBy = e.CreatedBy,
                                ProductStatus = e.ProductStatus == true ? true : false, // true: sẵn sàng
                                ProductName = e.ProductName,
                                priceProduct = e.Price,
                                quantityProduct = e.Quantity,
                                money = e.Price * e.Quantity,
                            };

                total = query.Count();

                //return
                return query.Take(total).ToList();
            }
        }
    }
}
